//
//  SmartLifeViewController.m
//  SmartLife
//
//  Created by Lawrence on 15/11/3.
//  Copyright © 2015年 Lawrence. All rights reserved.
//

#import "SmartLifeViewController.h"
#import "SmartLifeList.h"
#import "QRCodeViewController.h"
#import "CityWeatherList.h"
#import "AMapNavViewController.h"
#import "PhoneQueryViewController.h"
#import "QRCodeViewController.h"
#import "NotesViewController.h"


#import "PlistHelper.h"

#import <KxMenu.h>
#import <RESideMenu.h>


@interface SmartLifeViewController ()<UITableViewDelegate,UITableViewDataSource,UISearchBarDelegate,UISearchDisplayDelegate>{
   
    NSArray *cellItems;
    NSArray *cellName;
    NSArray *cellImage;
    
}



@property (nonatomic,strong) UISearchBar *searchBar;
@property (nonatomic,strong) UITableView *tableView;

@property(nonatomic,strong) AMapNavViewController *AmapNavi;

@end

@implementation SmartLifeViewController

-(instancetype)initStoryboardWithframe:(CGRect)frame{
    UIStoryboard *storyBoard=[UIStoryboard storyboardWithName:@"SmartLifeList" bundle:nil];
    SmartLifeViewController *smart=[storyBoard instantiateViewControllerWithIdentifier:@"SmartLifeListView"];
    smart.view.frame=frame;
    return smart;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    NSLog(@"%f",self.view.frame.size.height);
    NSLog(@"%f",self.view.frame.size.width);
    
    [self initData];
    [self initNav];
    [self initTableView];
    [self registerNib];
    [self initSearchBar];
    [self addGesture];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}

-(void)initData{
    
    cellName=@[
               NSLocalizedString(@"天气",nil),
               NSLocalizedString(@"地图",nil),
               NSLocalizedString(@"电话归属地查询",nil),
               NSLocalizedString(@"二维码",nil),
               NSLocalizedString(@"便签",nil)
               ];
    
    cellImage=@[
                @"weather",
                @"map",
                @"search",
                @"QrCode",
                @"EventNote"
                ];
}

-(void)registerNib{
    
    UINib *nib=[UINib nibWithNibName:@"SmartLifeListCell" bundle:nil];
    [self.tableView registerNib:nib  forCellReuseIdentifier:@"SmartLifeListCell"];
}

-(void)initNav{
    
    UINavigationItem *navItem=self.navigationItem;
    navItem.title=NSLocalizedString(@"SmartLife",nil);
    
    
    UIBarButtonItem *bbi=[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(initKxMenu:)];
    navItem.rightBarButtonItem=bbi;
    
    
    //设置tag用于回收和打开下拉菜单
    navItem.rightBarButtonItem.tag=10;
    
    UIBarButtonItem *lbbi=[[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"menu"] style:UIBarButtonItemStylePlain target:self action:@selector(presentLeftMenuViewController:)];
    navItem.leftBarButtonItem=lbbi;
    
    //self.navigationController.navigationBar.barTintColor=[UIColor blueColor];

    
}
-(void)initKxMenu:(id)sender{
    if (self.navigationItem.rightBarButtonItem.tag==10) {
        NSArray *menuItems=@[
                             [KxMenuItem menuItem:NSLocalizedString(@"扫一扫",nil)   image:[UIImage imageNamed:@"qr_BarCode"] target:self action:@selector(pushQrCodeViewController)],
                             [KxMenuItem menuItem:NSLocalizedString(@"快递单号",nil)    image:[UIImage imageNamed:@"BarCode"] target:self action:nil]
                             ];
        for (KxMenuItem *item in menuItems) {
            item.foreColor=[UIColor colorWithRed:47/255.0f green:112/255.0f blue:255/255.0f alpha:1.0];
            item.alignment=NSTextAlignmentCenter;
        }
        
        CGRect rect=self.navigationController.navigationBar.frame;
        CGFloat height=0.0;
        //判断系统版本号为多少
        if ([[[UIDevice currentDevice] systemVersion] floatValue]>=7.0) {
            height=64.0;
        }
        else{
            height=0.0;
        }
        [KxMenu showMenuInView:self.view fromRect:CGRectMake(rect.origin.x+self.navigationController.navigationBar.frame.size.width/2-44, rect.origin.y-10,rect.size.width,height) menuItems:menuItems];
        self.navigationItem.rightBarButtonItem.tag=20;
    }
    else{
        [KxMenu dismissMenu];
        self.navigationItem.rightBarButtonItem.tag=10;
    }
}
-(void)pushQrCodeViewController{
    QRCodeViewController *qrView=[[QRCodeViewController alloc] init];
    [self.navigationController pushViewController:qrView animated:YES];
}

-(void)initSearchBar{
    
    UISearchBar *tempSearchbar=[[UISearchBar alloc] initWithFrame:CGRectMake(0,0, self.tableView.frame.size.width,60)];
    tempSearchbar.placeholder=NSLocalizedString(@"Input your phone number",nil);
    
    self.searchBar=tempSearchbar;
    self.searchBar.delegate=self;
    self.tableView.tableHeaderView=self.searchBar;
    
    [self.searchBar sizeToFit];
}
-(void)initTableView{
    
    self.tableView=[[UITableView alloc] initWithFrame:CGRectMake(0,0,self.view.frame.size.width,self.view.frame.size.height) style:UITableViewStyleGrouped];
    
    self.tableView.delegate=self;
    self.tableView.dataSource=self;
    
    [self.view addSubview:_tableView];
}

#pragma mark 添加手势
-(void)addGesture{
    
    //添加点击手势
    UITapGestureRecognizer *tapGestureRec=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(SetNavItemTag)];
    tapGestureRec.cancelsTouchesInView=NO;
    [self.view addGestureRecognizer:tapGestureRec];
}

-(void)SetNavItemTag{
    self.navigationItem.rightBarButtonItem.tag=10;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return cellName.count;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 50.0f;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    SmartLifeList *cell=[tableView dequeueReusableCellWithIdentifier:@"SmartLifeListCell"];
    cell.labName.text=[cellName objectAtIndex:indexPath.row];
    cell.ImageTitle.image=[UIImage imageNamed:[cellImage objectAtIndex:indexPath.row]];
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
//[self.navigationController pushViewController:[cellItems objectAtIndex:indexPath.row] animated:YES];
    switch (indexPath.row) {
        case 0:
        {
            NSDictionary *dict=[[[PlistHelper GetCityInfoCode] objectForKey:@"China"] objectForKey:@"GuangDong"];
            CityWeatherList *list=[[CityWeatherList alloc] initCityWeatherListWithStoryboard:dict];
            [self.navigationController pushViewController:list animated:YES];
            
        }
            break;
        case 1:
        {
            self.AmapNavi=[[AMapNavViewController alloc] init];
            [self.navigationController pushViewController:_AmapNavi animated:YES];
        }
            break;
        case 2:{
            PhoneQueryViewController *phoneQuery=[[PhoneQueryViewController alloc] init];
            [self.navigationController pushViewController:phoneQuery animated:YES];
        }
            break;
        case 3:{
            QRCodeViewController *qrCodeView=[[QRCodeViewController alloc] init];
            [self.navigationController pushViewController:qrCodeView animated:YES];
        }
            break;
        case 4:{
            
            NotesViewController *notesView=[[NotesViewController alloc] init];
            [self.navigationController pushViewController:notesView animated:YES];
        }
            break;
        default:
            break;
    }
    
}

-(void)keyboardHide:(UITapGestureRecognizer *)tap{
    
    [_searchBar resignFirstResponder];
}
@end
